﻿using AutoMapper;
using Microsoft.Extensions.DependencyInjection;

namespace DiffuseEnjoyIdle.Core.Helper
{
    public static class AutoMapperHelper
    {
        private static IServiceProvider ServiceProvider;

        public static void UseStateAutoMapper(this IServiceProvider serviceProvider)
        {
            ServiceProvider = serviceProvider;
        }


        /// <summary>
        /// 单个对象映射
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static TDestination MapTo<TDestination>(this object source)
        {
            var mapper = ServiceProvider.GetRequiredService<IMapper>();

            return mapper.Map<TDestination>(source);
        }

        /// <summary>
        /// 多个对象映射到单个dto
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="source"></param>
        /// <param name="source2"></param>
        /// <returns></returns>
        public static TDestination MergeInto<TDestination>(this object source, object source2)
        {
            var mapper = ServiceProvider.GetRequiredService<IMapper>();
            return mapper.Map(source2, mapper.Map<TDestination>(source));
        }


    }
}
